package com.example.blogdesign.Controller;
import com.example.blogdesign.Service.IUserService;
import com.example.blogdesign.dto.LoginFormDTO;
import com.example.blogdesign.dto.RegisterDTO;
import com.example.blogdesign.dto.Result;
import com.example.blogdesign.dto.UserDTO;
import com.example.blogdesign.utils.UserHolder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private IUserService userService;


    /**
     * 发送手机验证码
     */
    @PostMapping("/code")
    public Result<?> sendCode(@RequestParam("phone") String phone) {
        // TODO 发送短信验证码并保存验证码
        return userService.sendCode(phone);
    }
    /**
     * 登录功能
     * @param loginForm 登录参数，包含手机号、验证码；或者手机号、密码
     */
    @PostMapping("/login")
    public Result<Object> login(@RequestBody LoginFormDTO loginForm){
        // TODO 实现登录功能
        return userService.login(loginForm);
    }
    /*
    * 注册
    *
    * */
    @PostMapping("/register")
    public Result<?> register(@RequestBody RegisterDTO registerDTO){
        return userService.register(registerDTO);
    }


    /*
    * 获取当前用户
    * */
    @GetMapping("/me")
    public Result<UserDTO> me(){
        // TODO 获取当前登录的用户并返回
        UserDTO user = UserHolder.getUser();
        return Result.ok(user);
    }

    /*
    * 登出
    *
    * */
    @GetMapping("/logout")
    public Result<?> logout(@RequestHeader(value = "authorization",defaultValue = "") String token){
        return userService.logout(token);
    }


    /*
    * 根据id查询用户
    * */
    @GetMapping("/{id}")
    public Result<UserDTO> queryUserById(@PathVariable("id") String userId){
        return userService.queryUserById(userId);
    }





}
